home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1932 < prev    next >
Encoding:
Text File  |  1996-08-05  |  5.0 KB  |  156 lines

  1. Path: cscsun3.larc.nasa.gov!hook
  2. From: hook@cscsun3.larc.nasa.gov (Ed Hook)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Weird perl-C interaction (rand)
  5. Date: 17 Jan 1996 22:28:02 GMT
  6. Organization: CSC/NASA Langley Research Center
  7. Distribution: world
  8. Message-ID: <4djt5i$fjn@reznor.larc.nasa.gov>
  9. References: <4djfjg$l45@decaxp.harvard.edu>
  10. Reply-To: hook@cscsun3.larc.nasa.gov
  11. NNTP-Posting-Host: cscsun3.larc.nasa.gov
  12. Keywords: perl, C, rand, random
  13.  
  14. In article <4djfjg$l45@decaxp.harvard.edu>, Zorro <berriz@husc.harvard.edu> writes:
  15. |> 
  16. |> 
  17. |> I tried the following in a perl script to generate seeds to be given
  18. |> as arguments in calls to a C program:
  19. |> 
  20. |>   $seed = int(rand((2**31) -1)) + 1;
  21. |> 
  22. |> The C program stores this value in its own local variable "seed" and,
  23. |> of course, uses it to seed its random number generator:
  24. |> 
  25. |>   srandom(seed);
  26. |> 
  27. |> Then, the C program calls random(), and takes the modulo 32768 of the
  28. |> returned value.
  29. |> 
  30. |>   u = random() % 32768;
  31. |> 
  32. |> To my utter wilderment the resulting number u is *always* the same,
  33. |> even though the seeds produced by the perl script are always
  34. |> different.
  35. |> 
  36. |> I got around the problem by switching the perl code to
  37. |> 
  38. |>   $seed = int(rand((2**14) -1)) + 1;
  39. |> 
  40. |> ...but I'm clueless as to what's going on.  Does anybody know?  More
  41. |> to the point, is my "solution" OK?
  42. |> 
  43. |> FWIW, I'm running this on an IBM RS/6000, under AIX 3.2.5.  Below are
  44. |> toy versions of the perl script ("mumble") and the C program
  45. |> ("frotz.c", compiled to the executable "frotz"); they exhibit the
  46. |> problem described above.  I've also included mumble's output.
  47. |> 
  48. |> Thanks in advance,
  49. |> 
  50. |> Z.
  51. |> 
  52. |> 
  53. |> ... mumble ..................................................................
  54. |> 
  55. |> #!/usr/local/bin/perl -w
  56. |> 
  57. |> srand(time|$$);
  58. |> 
  59. |> for(1..10) {
  60. |>   $seed = int(rand((2**31) -1)) +1;
  61. |>   system("frotz $seed");
  62. |> }
  63. |> 
  64. |> ..........................................................................
  65. |> 
  66. |> /* frotz.c */
  67. |> void main(int argc, char *argv[]) {
  68.  
  69.   Oops -- you've misspelled "int main(int argc, char *argv[]) {"
  70. |> 
  71. |>   int x;
  72. |>   unsigned int seed;
  73. |> 
  74. |>   seed = (unsigned) atoi(argv[1]);
  75. |>   srandom(seed);
  76. |>   x = random();
  77. |> 
  78. |>   printf("%12d %12d %6d\n", seed, x, x % 32768);
  79. |> }
  80. |> 
  81. |> ..........................................................................
  82. |> $ mumble
  83. |>    494862336   1155565115   1595
  84. |>    614662144    884639291   1595
  85. |>    963575808   1387824699   1595
  86. |>   2010775552   1296303675   1595
  87. |>   1405616128   1142359611   1595
  88. |>     84934656    994772539   1595
  89. |>   1688469504    272434747   1595
  90. |>   1452736512   1738311227   1595
  91. |>    402128896   1452213819   1595
  92. |>   1499332608    789448251   1595
  93.  
  94.   It looks like you've uncovered a serious problem -- I modified 'frotz'
  95.   so that it dumps out the first 10 values returned by 'random()', along
  96.   with their reductions mod 32768. It appears that, regardless of the
  97.   seed value, the reductions are always the *same* sequence; here are three
  98.   examples:
  99.  
  100. seed =   1184563200 :
  101.            765888059   1595
  102.            770629765  24709
  103.           1234495940  27076
  104.            959331147  15179
  105.           1640054444  16044
  106.            414280009  26953
  107.           1821691921  20497
  108.           1815332352  17920
  109.           1396069928  22056
  110.            715464796   8284
  111. seed =    434831360 :
  112.           1441695291   1595
  113.           1543430277  24709
  114.            191687108  27076
  115.           1713257291  15179
  116.            749813420  16044
  117.           1770613065  26953
  118.           1160040465  20497
  119.             10733056  17920
  120.           1175344680  22056
  121.            194846812   8284
  122. seed =    987693056 :
  123.           1582335547   1595
  124.           1931665541  24709
  125.             13822404  27076
  126.             62536523  15179
  127.           1036861100  16044
  128.            682584393  26953
  129.            132698129  20497
  130.            186631680  17920
  131.           1102337576  22056
  132.           1846222940   8284
  133.  
  134.   This doesn't seem to square very well with the claims put forth in the
  135.   AIX (4.1.3) man pages:
  136.  
  137.    "The random and srandom subroutines have almost the same calling sequence
  138.     and initialization properties as the rand and srand subroutines. The
  139.     difference is that the rand subroutine produces a much less random
  140.     sequence; in fact, the low dozen bits generated by the rand subroutine
  141.     go through a cyclic pattern. All the bits generated by the random
  142.     subroutine are usable. For example, random( )&01 produces a random
  143.     binary value." (Quoted without permission ... obviously.)
  144.  
  145.   Although the behavior discovered above doesn't make the final claim untrue,
  146.   I would certainly wonder about the "randomness" of a sequence that always
  147.   looks exactly the same modulo a not-too-large power of 2 ...
  148.  
  149.   Any other comments ?
  150.  
  151. -- 
  152.  Ed Hook                              |       Coppula eam, se non posit
  153.  Computer Sciences Corporation        |         acceptera jocularum.
  154.  NASA Langley Research Center         | Me? Speak for my employer?...<*snort*>
  155.  Internet: hook@cscsun3.larc.nasa.gov |        ... Get a _clue_ !!! ...
  156.